package org.example.student_dormitory.controller;

import net.sf.json.JSONObject;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.service.BuildingManagerService;
import org.example.student_dormitory.service.impl.BuildingManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/buildingManager")
public class BuildingManagerServlet extends HttpServlet {
    private BuildingManagerService buildingManagerService = new BuildingManagerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求响应编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");

        // 获取前端请求数据
        String method = req.getParameter("method");
        switch (method){
            case "paging" ://分页
                paging(req,resp);
                break;
            case "loadAdmin" :
                loadManager(req,resp);
                break;
            case "add" :
                addBuilding(req,resp);
                break;
            case "edit":
                editBuilding(req,resp);
                break;
            case "delete" ://删除
                deleteAdmin(req,resp);
                break;
        }
    }

    private void deleteAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Result result = buildingManagerService.deleteById(id);

        returnData(resp,result);
    }

    private void editBuilding(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String buildingInfo = req.getParameter("buildingInfo");
//        String adminId = req.getParameter("adminId");

        Result result = buildingManagerService.updateBuilding(id, buildingInfo);
        returnData(resp,result);
    }

    private void addBuilding(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String explain = req.getParameter("explain");
        String adminId = req.getParameter("adminId");

        Result result = buildingManagerService.addBuilding(name, explain, adminId);
        returnData(resp,result);
    }

    private void loadManager(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //加载管理员信息
        Result result = buildingManagerService.loadAmins();
        returnData(resp,result);
    }

    private void returnData(HttpServletResponse resp, Result result) throws IOException {
        String json_str = JSONObject.fromObject(result).toString();

        resp.getWriter().write(json_str);
    }

    private void paging(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        String key = req.getParameter("key");
        String value = req.getParameter("value");

        Result result = buildingManagerService.loadBuildings(page,pageSize,key,value);
        returnData(resp,result);
    }
}
